package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC890 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<String> findAndReplacePattern(String[] words, String pattern) {
            List<String> res = new ArrayList<>();
            for (String word : words) {
                Map<Character, Character> pmp = new HashMap<>();
                Map<Character, Character> wmp = new HashMap<>();
                int i = 0;

                for (; i < word.length(); i++) {
                    if (pmp.containsKey(pattern.charAt(i))) {
                        if (pmp.get(pattern.charAt(i)) != word.charAt(i)) {
                            i = 0;
                            break;
                        }
                    } else pmp.put(pattern.charAt(i), word.charAt(i));

                    // wmp
                    if (wmp.containsKey(word.charAt(i))) {
                        if (wmp.get(word.charAt(i)) != pattern.charAt(i)) {
                            i = 0;
                            break;
                        }
                    } else wmp.put(word.charAt(i), pattern.charAt(i));
                }

                if (i >= word.length()) res.add(word);
            }

            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
//        输入：words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
//        输出：["mee","aqq"]
        String[] words = {"abc", "deq", "mee", "aqq", "dkd", "ccc"};
        System.out.println(solution.findAndReplacePattern(words, "abb"));
    }

    public static void main(String[] args) throws Exception {
        LC890 an = new LC890();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
